﻿using System;
using System.IO;

public static class PathTool
	{
		public static string AssetsFlag = "Assets/";

		public static string AssetBundleFlag = "AssetBundle/";

        #region 扩展string方法
        public static string FormatPath(this string path){
            return FormationPath(path);
        }

        #endregion

		//符号转换
		public static string ToForwardSlash (string path)
		{
			return path.Replace (@"\", @"/");
		}

		//符叼转换
		public static string ToSingleForwardSlash (string path)
		{
			return path.Replace (@"//", @"/");
		}

		/// <summary>
		/// 将路径格式化
		/// </summary>
		/// <returns>The path.</returns>
		/// <param name="path">Path.</param>
		public static string FormationPath(string path){
			path = ToForwardSlash (path);
			path = ToSingleForwardSlash (path);
			return path;
		}

		/// <summary>
		/// 移除 flag之前的路径字符   比如传过来    e:test/assets/texture/1.jpg flag为  assets/  那么会返回    assets/texture/1.jpg
		/// </summary>
		/// <returns>The to.</returns>
		/// <param name="path">Path.</param>
		/// <param name="flag">Flag.</param>
		public static string RemoveTo(string path,string flag){
			path = FormationPath (path);
			int index = path.LastIndexOf (flag);
			if(index > -1)
				return  path.Substring (index);
			return path;
		}

		/// <summary>
		/// 移除 flag之前包括flag的路径字符   比如传过来    e:test/assets/texture/1.jpg flag为  assets/  那么会返回    texture/1.jpg
		/// </summary>
		/// <returns>The to1.</returns>
		/// <param name="path">Path.</param>
		/// <param name="flag">Flag.</param>
		public static string RemoveTo1(string path,string flag){
			path = FormationPath (path);
			int index = path.LastIndexOf (flag);
			if (index > -1) {
				index += flag.Length;
				return  path.Substring (index);
			}
			return path;
		}


		/// <summary>
		/// 从文件系统移除一个路径的文件或文件夹
		/// </summary>
		/// <param name="path">Path.</param>
		public static void RemoveFile(string path){
			path = FormationPath (path);
			if (Directory.Exists (path)) {
				string[] files =  Directory.GetFiles (path);
				for(int i=0;i<files.Length;i++){
					File.Delete (files[i]);
				}
				Directory.Delete (path);
			}
		}

	}


